﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using Cosmetic.DataEntity;
using System.Collections.Generic;

public partial class Controls_BrandOrderByProducts : System.Web.UI.UserControl
{
	private BrandManager m_BrandManager			= new BrandManager();
	private ProductManager m_ProductManager		= new ProductManager();

	private int m_ShowCount		= 10;
	public int ShowCount
	{
		get { return m_ShowCount; }
		set { m_ShowCount = value; }
	}


	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			this.BindBrand();
		}
	}

	private void BindBrand()
	{
		Dictionary<Brand, int> theBrandDict	= new Dictionary<Brand,int>();
		foreach (Brand theBrand in this.m_BrandManager.GetAll())
		{
			List<Product> theProductList	= this.m_ProductManager.GetProductByBrand(theBrand);
			theBrandDict.Add(theBrand, theProductList.Sum(theProduct => theProduct.Comments.Count));
		}
		
		this.rptBrand.DataSource	= theBrandDict.OrderByDescending(theBrand => theBrand.Value).Take(ShowCount);
		this.rptBrand.DataBind();
	}
}
